<?php
namespace app\index\controller;
use think\Session;
use think\Controller;
use think\Db;
class Appo extends Controller
{
    public function List(){
    	if (session("login")!=1) return $this->error("请先登录");
    	$result=Db::query("SELECT a.appo_id,a.name as 'c_name',d.name as 'd_name',c.name as 'md_name',ask,state,a.time,a.tel FROM appointment a left join doctor d left join clinic c on d.clinic=c.id on a.doctor_id=d.id where a.doctor_id=".session("uid"));
    	$this->assign("arr",$result);
        return $this->fetch("list");
    }
    public function Del(){
    	if (session("login")!=1) return $this->error("请先登录");
    	$result=db('appointment')->where('appo_id',input("id"))->delete();
    	if($result==1) return $this->success('删除成功');
    	else return $this->error('删除失败');
    }
    public function Do(){
        if (session("login")!=1) return $this->error("请先登录");
        if (isset($_POST["AddCustomer"])) { //注册表单提交
            //先处理照片
            $file = request()->file('photo');
            if($file){
                $info = $file->rule("md5")->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    //windows
                    $photoStr="/uploads/".str_replace('\\','/',$info->getSaveName());
                    //linux
                    //$photoStr="/uploads/".$info->getSaveName();    
                }else{
                    $photoStr="";
                    return "图片上传失败！";
                }
            }
            //再处理性别
            if ($_POST["gender"]=="female") {
                $gender=0;
            } else {
                $gender=1;
            }
            $user=[
                "name"=>$_POST["name"],
                "gender"=>$gender,
                "tel"=>$_POST["tel"],
                "address"=>$_POST["address"],
                "firstdate"=>$_POST["firstdate"],
                "photo"=>$photoStr,
                "integral"=>$_POST["points"],
                "clinic"=>session("cid"),
                "doctor"=>$_POST["doctor"]
            ];
            $result=db('customer')->insertGetId($user);
            if($result){    //用户添加成功的前提下
                if($_POST["needcase"]=="true"){ //同时添加病例处理
                    $case=[
                        "uid"=>$result,
                        "yid"=>$_POST["doctor"],
                        "date"=>$_POST["firstdate"],
                        "content"=>$_POST["casecontent"],
                        "ask"=>$_POST["ask"],
                        "prescription"=>$_POST["prescription"],
                        "clinic"=>session("cid")
                    ];
                    $case_result=db("case")->insert($case);
                    if($_POST["needrevisit"]=="true"){ //需要复诊处理
                        db('customer')->where("id",$result)->setField("lastdate",$_POST["revisitdate"]);
                    }
                    if ($case_result) {
                    	$res=db("appointment")->where("appo_id",input("id"))->update(array("state"=>1));
                        return $this->success("用户注册成功","/Customer/List");
                    }else{
                        return $this->error("用户注册失败");
                    }
                }else{
                	$res=db("appointment")->where("appo_id",input("id"))->update(array("state"=>1));
                    return $this->success("用户注册成功","/Customer/List");
                }
            }else {
                return $this->error("用户注册失败");
            }

        }else{
        	$res=db("appointment")->where("appo_id",input("id"))->find();
        	$this->assign("a",$res);
    		return $this->fetch("do");
        }
        
    }
}


